import es
import random

from wcs.libs.items import items
from wcs.libs.players import players

itemName = 'Dust of Appearance'
item = items.addItem(itemName)
item.setMaxCount(1)
item.aliveToUse = True
item.setCost(3000)
item.setGroup("attack")
item.setDescription("Your bullets reveal hidden enemies and give true sight.")
        
def player_hurt(event_var):
    """
    Executed when a player is hurt. We want to see if the attacker has this item
    purchased; if so, then ensure that the stealthed character is revealed given
    a 50% chance dice throw. If they are revealed, then create a delay to send
    them back to their old alpha after 5 seconds.
    """
    attacker = int(event_var["attacker"]) if event_var["attacker"] else 0
    userid = int(event_var["userid"])
    if attacker and attacker != userid:
        wcsAttacker = players[attacker]
        if wcsAttacker.getItem(itemName).count:
            if random.randint(1, 100) <= 50:
                amount = 20
                wcsUserid = players[userid]
                wcsUserid.fade(wcsUserid.minAlpha + amount, 1)
                wcsUserid.cancelDelayed("fadeToNormal")
                wcsUserid.delayedName(5, "fadeToNormal", wcsUserid.fade, (
                                                             wcsUserid.minAlpha, 
                                                             1))
                text = ("#greenDust of Appearance #default- #lightgreenYou" +
                        " have revealed #green%s")
                es.tell(attacker, "#multi", text % event_var['es_username'])
                text = ("#greenDust of Appearance #default- #lightgreenYou" +
                        " have been revealed by #green%s")
                es.tell(userid, "#multi", text % event_var['es_attackername'])